﻿using System;
using DotNetNeural.Data.Clusters;

namespace DotNetNeural.Utils
{
    /// <summary>
    /// Provides methods of cluster generation
    /// </summary>
    public class ClusterGenerator
    {
        public const float GeneratorPrecision = 0.0000001f;

        /// <summary>
        /// Generates cluster using given parameter
        /// </summary>
        /// <param name="center">Cluster center</param>
        /// <param name="itemsCount">Total number of items in cluster</param>
        /// <param name="dispersion">Maximum distance from cluster center to each cluster's item</param>
        public Cluster Generate(float[] center, int itemsCount, float dispersion)
        {
            if (center == null)
                throw new NullReferenceException("Illegal null-reference cluster center");

            if (dispersion < 0 || itemsCount < 0)
                throw new ArgumentException("Illegal negative dispersion or items count in cluster");

            VectorGenerator vecGenerator = new VectorGenerator();
            Cluster cluster = new Cluster(center)
            {
                Dispersion = dispersion
            };

            for (int i = 0; i < itemsCount; ++i)
            {
                float[] vec = vecGenerator.Generate(center, dispersion);

                while (cluster.Contains(vec))
                {
                    vec = vecGenerator.Generate(center, dispersion);
                }

                cluster.Add(vec);
            }

            return cluster;
        }
    }
}
